package com.whfc.ms.api.emp.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpConfessDTO;
import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.param.AppEmpConfessAddParam;
import com.whfc.emp.param.AppEmpConfessEditParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppEmpConfessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-02 16:31
 */
@RestController
@RequestMapping("/ms/api/emp/confess")
public class AppEmpConfessController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAppEmpConfessService msAppEmpConfessService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("empId") Integer empId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取安全交底列表，loginer:{},empId:{},pageNum:{}, pageSize:{}", user.getUsername(), empId, pageNum, pageSize);
        PageData<AppEmpConfessDTO> pageData = msAppEmpConfessService.list(empId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }


    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppEmpConfessAddParam param) {
        logger.info("添加安全交底信息，loginer:{},param:{}", user.getUsername(), param);
        msAppEmpConfessService.add(param);
        return ResultUtil.success();
    }


    @PostMapping("/edit")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppEmpConfessEditParam param) {
        logger.info("修改安全交底信息，loginer:{},param:{}", user.getUsername(), param);
        msAppEmpConfessService.edit(param);
        return ResultUtil.success();
    }


    @PostMapping("/del/{confessId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("confessId") Integer confessId) {
        logger.info("删除安全交底信息，loginer:{},confessId:{}", user.getUsername(), confessId);
        msAppEmpConfessService.del(confessId);
        return ResultUtil.success();
    }

    @GetMapping("/user/list")
    public Result getUserList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam(value = "keyword",required = false) String keyword) {
        logger.info("获取用户列表，loginer:{}, deptId:{}, keyword:{}", user.getUsername(), deptId, keyword);
        List<AppEmpDTO> list = msAppEmpConfessService.getUserList(deptId, keyword);
        return ResultUtil.success(new ListData<>(list));
    }


}
